<?php

namespace App\Service\OpenShop\Feishu;

use App\Models\FeishuCallingCard\FeishuCallingCard;
use App\Models\FeishuCallingCard\FeishuCallingCardPosterBackground;
use App\Service\Common\OssService;
use App\Service\OpenShop\Pdf\PdfMakerService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Kuafu\File\FileUtil;

class QualificationImageService
{

    const TYPE_AUTHORIZATION = 1; //夸父授权书
    const TYPE_COLLEGE       = 2; //夸父结业证书
    const TYPE_WX_CALLING_CARD = 3; // 微信名片

    //授权书 certificate of authorization
    const FILE_NAME_PREFIX = 'kuafu_tiger_open_shop';
    const FONT = '96/ec/2276d8e77453e0e44f1e9633ee513396f2eeabb3d330f9e9306343b25fd9.ttf';
    const CERTIFICATE_OF_AUTHORIZATION = 'a9/73/3b13cd0cd91b0ba3ab6b7e74da912b098c4f8ded268006a02c87a9a993c5.png';
    const CERTIFICATE_OF_AUTHORIZATION_COMPANY = 'ae/7c/bf852a4b384fc657b867e9d078fda7c75570415ace30b0347c227a358d01.png';
    const CERTIFICATE_OF_AUTHORIZATION_SEAL = '49/45/aaaf5486498fbf13112f64fb15ef03070f17d935389c740e2650fd861637.png';
    const CERTIFICATE_OF_AUTHORIZATION_NAME = '夸父开店授权书';
    const CONTRACT_PREFIX = 'KF-';
    const FOND_STYLE_COLOR = '#3A3A3A';
    const FOND_STYLE_SIZE  = 26;

    const COLLEGE_FILE_NAME_PREFIX = 'kuafu_tiger_college';
    const CERTIFICATE_OF_COLLEGE = 'f4/9e/bd587f8b131d3bb4f96bca4564273c659fb6e28960d4ee2817b9b341ddb6.png';
    const CERTIFICATE_OF_COLLEGE_SEAL = '56/68/6941338b49f1de25ae41bb4c6190713c3176462e5d990173816420a004ca.jpeg';

    const WX_CALLING_CARD_PREFIX = 'kuafu_wx_calling_card';
    const WX_CALLING_CARD_BG = 'c3/e9/944b05f7c782d51e8ac4729594be797f6dae2c14dd20c65f4e1770e6d652.png';
    const WX_CALLING_CARD_PHONE = "29/b0/4a5abf7161e53bf5776a8bd4f7312b331d971a910ee5ca682df9b467851d.png?x-oss-process=image/resize,w_20";
    const WX_CALLING_CARD_LOCATION = "f5/66/c2d30cdc09b3d14b50044bfcf19fe1f171b2abe29e9ef7dad3edcb48295d.png?x-oss-process=image/resize,w_20";

    const TYPE_PATH = [
        self::TYPE_AUTHORIZATION => self::FILE_NAME_PREFIX,
        self::TYPE_COLLEGE       => self::COLLEGE_FILE_NAME_PREFIX,
        self::TYPE_WX_CALLING_CARD => self::WX_CALLING_CARD_PREFIX,
    ];


    /**
     * 制作授权书，并且返回oss的url
     * @return array
     */
    public static function shareImage($code, $name, $num, $address, $startTime, $endTime, $time = 0)
    {
        if (!$time) {
            $time = time();
        }
        static::prepareResource();
        $addressArr = explode('-', $address);
        $city = ($addressArr[0] ?? '') . ($addressArr[1] ?? '');
        $area = $addressArr[1] ?? '';
        ini_set('memory_limit','2048M');
        $img = Image::make(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . 'certificate.png');
        $img->text(static::CONTRACT_PREFIX . $code, 910, 804, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($name, 360, 920, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($num, 696, 920, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($city, 416, 993, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($area, 259, 1068, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $startTime), 342, 1214, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $startTime), 468, 1214, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $startTime), 548, 1214, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $endTime), 716, 1214, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $endTime), 834, 1214, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $endTime), 912, 1214, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $time), 769, 1495, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $time), 897, 1495, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $time), 978, 1495, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        });
        //$img->insert(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . 'certificateSeal.png', 'bottom-right', 200, 200);
        $saveFile = Str::random(10) . '.png';
        $img->save(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . $saveFile);
        $arr = OssService::uploadFile(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . $saveFile);
        @unlink(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . $saveFile);
        return $arr;
    }

    /**
     * 制作结业证书并返回url
     * @return array
     */
    public static function trainComplete($name, $startTime, $endTime, $time)
    {
        static::collegePrepareResource();

        ini_set('memory_limit','2048M');
        $img = Image::make(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX . '/' . 'certificate.png');
        $img->text($name, 226, 597, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $startTime), 388, 597, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $startTime), 504, 597, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $startTime), 579, 597, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $endTime), 682, 597, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $endTime), 162, 678, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $time), 520, 1073, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $time), 628, 1073, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $time), 698, 1073, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        });
        //$img->insert(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX . '/' . 'certificateSeal.png', 'bottom-right', 200, 200);
        $saveFile = Str::random(10) . '.png';
        $img->save(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX . '/' . $saveFile);
        $arr = OssService::uploadFile(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX . '/' . $saveFile);
        @unlink(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX . '/' . $saveFile);
        return $arr;
    }

    public static function shareCompanyImage($code, $name, $num, $address, $startTime, $endTime, $time = 0)
    {
        if (!$time) {
            $time = time();
        }
        static::prepareResource();
        $addressArr = explode('-', $address);
        $city = ($addressArr[0] ?? '') . ($addressArr[1] ?? '');
        $area = $addressArr[1] ?? '';
        ini_set('memory_limit','2048M');
        $img = Image::make(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . 'certificateCompany.png');
        $img->text(static::CONTRACT_PREFIX . $code, 910, 805, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($name, 350, 917, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($num, 250, 994, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($city, 790, 994, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text($area, 550, 1064, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $startTime), 352, 1216, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $startTime), 468, 1216, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $startTime), 540, 1216, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $endTime), 713, 1216, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $endTime), 825, 1216, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $endTime), 908, 1216, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('Y', $time), 790, 1495, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('m', $time), 901, 1495, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        })->text(date('d', $time), 980, 1495, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf'));
            $font->size(static::FOND_STYLE_SIZE);
            $font->color(static::FOND_STYLE_COLOR);
        });
        //$img->insert(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . 'certificateSeal.png', 'bottom-right', 200, 200);
        $saveFile = Str::random(10) . '.png';
        $img->save(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . $saveFile);
        $arr = OssService::uploadFile(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . $saveFile);
        @unlink(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX . '/' . $saveFile);
        return $arr;
    }

    /**
     * 微信小程序分享预览名片
     * @param string $avatar
     * @param string $name
     * @param string $position
     * @param string $phone
     * @param string $address
     * @return array
     * @throws \OSS\Core\OssException
     */
    public static function wxCallingCardImage(string $avatar, string $name, bool $showPosition, string $position, string $phone, string $address)
    {
        static::wxCallingCardPrepareSource();

        $img = Image::make(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX . '/' . 'wx_bg.png');

        // 头像
        $avatar .= '?x-oss-process=image/resize,w_120/circle,r_120';
        $img->insert($avatar, 'top-right', 40, 110);

        // 名字
        $img->text($name, 38, 132, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
            $font->size(34);
            $font->color("#333333");
        });

        $img->text("夸父炸串", 38, 174, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
            $font->size(20);
            $font->color("#333333");
        });

        // 职位
        if ($showPosition) {
            $img->text($position, 160, 173, function ($font) {
                $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
                $font->size(18);
                $font->color("#333333");
            });
        }

        // 手机icon
        $img->insert(static::tempFileName("phone.png", self::TYPE_WX_CALLING_CARD), 'left-top', 38, 330);

        // 手机号
        $img->text($phone, 70, 346, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
            $font->size(18);
            $font->color("#333333");
        });

        // 地址icon
        $img->insert(static::tempFileName("location.png", self::TYPE_WX_CALLING_CARD), 'left-top', 38, 380);

        // 地址
        $startAddress = $endAddress = '';
        if (mb_strlen($address) > 22) {
            $startAddress = mb_substr($address, 0, 22);
            $endAddress = mb_substr($address, 22, -1);
        }

        $img->text($startAddress, 70, 398, function ($font) {
            $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
            $font->size(18);
            $font->color("#333333");
        });

        if ($endAddress) {
            $img->text($endAddress, 70, 424, function ($font) {
                $font->file(static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
                $font->size(18);
                $font->color("#333333");
            });
        }

        $saveFile = Str::random(10) . '.png';
        $img->save(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX . '/' . $saveFile);
        $arr = OssService::uploadFile(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX . '/' . $saveFile);
        @unlink(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX . '/' . $saveFile);
        return $arr;
    }

    /**
     * 固定的文件在本地的临时地址
     * @param $name
     * @param int $type 证书类型
     * @return string
     */
    protected static function tempFileName($name, int $type = self::TYPE_AUTHORIZATION): string
    {
        $path = self::TYPE_PATH[$type] ?? '';

        return sys_get_temp_dir() . '/' . $path . '/' . $name;
    }

    /**
     * 下载oss文件到本地
     * @param string $url
     * @param string $localPath
     */
    protected static function downloadFile(string $url, string $localPath)
    {
        @unlink($localPath);
        file_put_contents($localPath, file_get_contents(FileUtil::domain() . '/' . $url));
    }


    protected static function prepareResource()
    {
        if (!is_dir(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX)) {
            @unlink(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX);
            mkdir(sys_get_temp_dir() . '/' . static::FILE_NAME_PREFIX, 0777, true);
        }
        if (!static::checkSha256('certificate.png', static::CERTIFICATE_OF_AUTHORIZATION)) {
            static::downloadFile(static::CERTIFICATE_OF_AUTHORIZATION, static::tempFileName('certificate.png'));
        }
        if (!static::checkSha256('certificateCompany.png', static::CERTIFICATE_OF_AUTHORIZATION_COMPANY)) {
            static::downloadFile(static::CERTIFICATE_OF_AUTHORIZATION_COMPANY, static::tempFileName('certificateCompany.png'));
        }
        if (!static::checkSha256('certificateSeal.png', static::CERTIFICATE_OF_AUTHORIZATION_SEAL)) {
            static::downloadFile(static::CERTIFICATE_OF_AUTHORIZATION_SEAL, static::tempFileName('certificateSeal.png'));
        }
        if (!static::checkSha256('certificateFont.ttf', static::FONT)) {
            static::downloadFile(static::FONT, static::tempFileName('certificateFont.ttf'));
        }
    }


    //结业证书文件准备
    protected static function collegePrepareResource()
    {
        if (!is_dir(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX)) {
            @unlink(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX);
            mkdir(sys_get_temp_dir() . '/' . static::COLLEGE_FILE_NAME_PREFIX, 0777, true);
        }
        if (!static::checkSha256('certificate.png', static::CERTIFICATE_OF_AUTHORIZATION, self::TYPE_COLLEGE)) {
            static::downloadFile(static::CERTIFICATE_OF_COLLEGE, static::tempFileName('certificate.png', self::TYPE_COLLEGE));
        }
        if (!static::checkSha256('certificateSeal.png', static::CERTIFICATE_OF_AUTHORIZATION_SEAL, self::TYPE_COLLEGE)) {
            static::downloadFile(static::CERTIFICATE_OF_COLLEGE_SEAL, static::tempFileName('certificateSeal.png', self::TYPE_COLLEGE));
        }
        if (!static::checkSha256('certificateFont.ttf', static::FONT, self::TYPE_COLLEGE)) {
            static::downloadFile(static::FONT, static::tempFileName('certificateFont.ttf', self::TYPE_COLLEGE));
        }
    }

    // 微信名片图片准备
    protected static function wxCallingCardPrepareSource()
    {
        if (!is_dir(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX)) {
            @unlink(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX);
            mkdir(sys_get_temp_dir() . '/' . static::WX_CALLING_CARD_PREFIX, 0777, true);
        }

        if (!static::checkSha256('wx_bg.png', static::WX_CALLING_CARD_BG, self::TYPE_WX_CALLING_CARD)) {
            static::downloadFile(static::WX_CALLING_CARD_BG, static::tempFileName('wx_bg.png', self::TYPE_WX_CALLING_CARD));
        }

        if (!static::checkSha256('certificateFont.ttf', static::FONT, self::TYPE_WX_CALLING_CARD)) {
            static::downloadFile(static::FONT, static::tempFileName('certificateFont.ttf', self::TYPE_WX_CALLING_CARD));
        }

        if (!static::checkSha256('phone.png', self::WX_CALLING_CARD_PHONE, self::TYPE_WX_CALLING_CARD)) {
            static::downloadFile(self::WX_CALLING_CARD_PHONE, static::tempFileName('phone.png', self::TYPE_WX_CALLING_CARD));
        }

        if (!static::checkSha256('location1.png', self::WX_CALLING_CARD_LOCATION, self::TYPE_WX_CALLING_CARD)) {
            static::downloadFile(self::WX_CALLING_CARD_LOCATION, static::tempFileName('location.png', self::TYPE_WX_CALLING_CARD));
        }
    }

    /**
     * 检查文件指纹
     * @param $name
     * @param $url
     * @param int $type
     * @return bool
     */
    protected static function checkSha256($name, $url, int $type = self::TYPE_AUTHORIZATION): bool
    {
        $tempFilename = static::tempFileName($name, $type);
        if (!is_readable($tempFilename)) {
            return false;
        }
        $sha256 = substr(str_replace('/', '', $url), 0, 64);
        if ($sha256 != hash_file('sha256', $tempFilename)) {
            return false;
        }
        return true;
    }
}
